package core.bmp.worker.filter;

import java.io.File;

import core.bmp.AbsMatrixPixWorker;
import core.bmp.model.Pixel;
import core.bmp.utils.PixelUtil;

public class AverageFilteringWorker extends AbsMatrixPixWorker {

	@Override
	protected File onPrepareImageFile() {
		return null;
	}

	@Override
	protected int[][] work(int[][] argbs) {
		int height = argbs.length;
		int width = argbs[0].length;

		int[][] newArgbs = argbs.clone();

		int sum = 0;
		for (int x = 1; x < width - 1; x++) {
			for (int y = 1; y < height - 1; y++) {
				sum = 0;
				sum += getGray(argbs[x - 1][y - 1] & 0xFF);
				sum += getGray(argbs[x - 1][y] & 0xFF);
				sum += getGray(argbs[x - 1][y + 1] & 0xFF);

				sum += getGray(argbs[x][y - 1] & 0xFF);
				sum += getGray(argbs[x][y] & 0xFF);
				sum += getGray(argbs[x][y + 1] & 0xFF);

				sum += getGray(argbs[x + 1][y - 1] & 0xFF);
				sum += getGray(argbs[x + 1][y] & 0xFF);
				sum += getGray(argbs[x + 1][y + 1] & 0xFF);

				Pixel pixel = PixelUtil.intToPixel(argbs[x][y]);
				PixelUtil.setGray(pixel, (byte) ((sum / 9) & 0xFF));
				newArgbs[x][y] = PixelUtil.pixelToInt(pixel);
			}
		}
		return newArgbs;
	}

}
